package org.feifei.quartz.job;

import org.feifei.order.service.AdoptOrderService;
import org.feifei.order.service.ProductOrderDetailService;
import org.feifei.order.service.ProductOrderService;
import org.feifei.quartz.constant.JobConstants;
import org.feifei.quartz.dto.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author forever
 * @Description: 执行的任务类，根据parameter中的type来区分任务的类型
 * @date 2021/12/19 14:42
 */
public class MainJob implements Job {

    @Resource
    private AdoptOrderService adoptOrderService;

    @Resource
    private ProductOrderService productOrderService;

    @Resource
    private ProductOrderDetailService productOrderDetailService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QuartzJobInfo jobInfo = (QuartzJobInfo) jobExecutionContext.getJobDetail().getJobDataMap().get("params");
        String type = jobInfo.getType();
        switch (type) {
            case JobConstants.OVER_TIME_NO_PAY_ADOPT : {
                Map<String, Object> params = jobInfo.getParams();
                // 获取订单id，通过订单id修改
                Object orderId = params.get("orderId");
                if(orderId!=null){
                    long oid = Long.parseLong(orderId.toString());
                    //根据old取消订单 = 设置state = -1
                    adoptOrderService.cancelOrder(oid);
                }
            } break;
            case JobConstants.OVER_TIME_NO_PAY_PRODUCT : {
                Map<String, Object> params = jobInfo.getParams();
                // 获取订单id，通过订单id修改
                Object orderId = params.get("orderId");
                if(orderId!=null){
                    long oid = Long.parseLong(orderId.toString());
                    //根据old取消订单 = 设置state = -1
                    productOrderService.cancelOrder(oid);
                    productOrderDetailService.cancelOrder(oid);
                }
            } break;
        }
    }
}
